/*
*Copyright 2021-2023 NERCIS
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/

package cn.ac.nercis.pes.community.controller.evaluation.onsite;

import cn.ac.nercis.pes.common.utils.PlaceholderUtils;
import cn.ac.nercis.pes.community.component.controls.ExpectedResultSelectControl;
import cn.ac.nercis.pes.community.component.controls.MenuListCell;
import cn.ac.nercis.pes.community.model.vo.MenuVO;
import cn.ac.nercis.pes.community.utils.AppStore;
import cn.ac.nercis.pes.community.utils.LogUtils;
import cn.ac.nercis.pes.model.dto.knowledge.KmExpectedResultDTO;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.ListView;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.ResourceBundle;

/**
 * 选择预期结果内容管理控制器
 *
 * @author zpy
 * @version 1.0.0
 */
@Slf4j
public class ChooseExpectedResultController implements Initializable {

    private final ToggleGroup toggleGroup = new ToggleGroup();
    @FXML
    private ListView<MenuVO> menuListView;
    @FXML
    private VBox resultListBox;
    private String assetName;
    private List<KmExpectedResultDTO> expectedResults;
    private String currentSystemId;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        menuListView.setCellFactory(cf->new MenuListCell());
        menuListView.getSelectionModel()
                .selectedItemProperty()
                .addListener((observable, oldValue, newValue) -> {
                    if (Objects.nonNull(newValue)) {
                        loadContent(newValue);
                    }
                });
    }

    public void initializeData(String systemId,String assetName, List<KmExpectedResultDTO> expectedResults) {
        LogUtils.info(log, "初始化选择预期结果内容...");
        this.currentSystemId = systemId;
        this.expectedResults = expectedResults;
        this.assetName = assetName;
        this.loadMenuData();
    }

    public String selectedExpectedResult() {
        return toggleGroup.getToggles()
                .stream()
                .filter(t -> t instanceof ExpectedResultSelectControl)
                .map(t -> (ExpectedResultSelectControl) t)
                .filter(ExpectedResultSelectControl::isSelected)
                .map(ExpectedResultSelectControl::getContent)
                .findFirst()
                .orElse("");
    }


    private void loadMenuData() {
        menuListView.getItems().clear();
        expectedResults.stream()
                .map(KmExpectedResultDTO::getAssetType)
                .filter(StringUtils::isNotBlank)
                .distinct()
                .forEach(assetType -> menuListView.getItems().add(new MenuVO(1,"",assetType,"folder-icon")));
        menuListView.getSelectionModel().select(0);
    }

    private void loadContent(MenuVO menuItem) {
        var currentSystem = AppStore.getCurrentSystem(this.currentSystemId);
        resultListBox.getChildren().clear();
        toggleGroup.getToggles().clear();
        expectedResults.stream()
                .filter(er -> StringUtils.equals(er.getAssetType(), menuItem.getTitle()))
                .forEach(er -> {
                    String content = PlaceholderUtils.builder()
                            .withAssetName(this.assetName)
                            .withSystemName(currentSystem.getSystemName())
                            .build()
                            .renderContent(er.getResult());
                    ExpectedResultSelectControl selectControl = new ExpectedResultSelectControl(content);
                    selectControl.setToggleGroup(toggleGroup);
                    toggleGroup.getToggles().add(selectControl);
                    resultListBox.getChildren().add(selectControl);
                });
    }

}